/*-----------------------------------------------------------------------------*/
/* InitDataBase.java                                                           */
/* Program that initializes the ecom databaseby creating beans			 */
/* Fabienne Boyer - Didier Donsez may 2006                                     */
/*-----------------------------------------------------------------------------*/

package ecom.client;

import ecom.beans.entity.Category;
import ecom.beans.entity.Clients;
import ecom.beans.entity.Productstore;
import ecom.beans.session.*;
import java.util.ArrayList;
import java.util.List;
import javax.transaction.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class InitDataBase {

  public static void main(String[] args) throws Exception {
  Context initialContext = null;
  UserTransaction utx = null;

  try {
    System.out.println("Initializing database...");


    // get JNDI initial context
	try {
	    initialContext = new InitialContext();
	} catch (Exception e) {
	    System.err.println("Cannot get initial context for JNDI: " + e);
	    System.exit(2);
	}

        try {
            utx = (UserTransaction) initialContext.lookup("clientTransaction");
        } catch (NamingException e) {
            System.err.println("Cannot lookup clientTransaction: " + e);
            System.exit(2);
        }

        EcomAdminRemote ecomAdminBean = null;
        EcomDataRemote ecomDataBean = null;
        //EcomCustomerRemote ecomCustomer = null;
        try {
            ecomAdminBean = (EcomAdminRemote) initialContext
                    .lookup(EcomAdminRemote.class.getName());
		System.out.println("Creation of admEcom bean done");
            ecomDataBean = (EcomDataRemote) initialContext.lookup(EcomDataRemote.class.getName());
                System.out.println("Creation of dataEcom bean done");
           // ecomCustomer = (EcomCustomerRemote) initialContext.lookup(EcomCustomerRemote.class.getName());
                System.out.println("Creation of CustomerEcom bean done");

        } catch (NamingException e) {
            System.err.println("Cannot get EcomAdminBean : " + e);
            System.exit(2);
        }

	utx.begin();
              utx.commit();

        System.out.println("Operations réalisées avec succés");

  } catch (Exception e) {
	 System.err.println("InitDataBase program get an exception " + e);
         
         e.printStackTrace();
       utx.rollback();
	 System.exit(2);
  }
  }



}
